package com.zhuoxin.dao.admin;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import com.zhuoxin.bean.model.Teacher;
import com.zhuoxin.bean.model.TeacherSO;
import com.zhuoxin.bean.system.SysDict;
import com.zhuoxin.dao.BaseDAOImpl;

public class TeacherDAOImpl extends BaseDAOImpl<Teacher, TeacherSO> implements TeacherDAO {

	@Override
	public String getHqlWhere(TeacherSO so) {
		return null;
	}

	@Override
	public void setCriteriaParams(Criteria criteria, TeacherSO so) {
		if(so.getDuty()!=null){
			criteria.createAlias("duty", "du");
			
			if(so.getDuty().getId()!=null){
				criteria.add(Restrictions.eq("du.id", so.getDuty().getId()));
			}
			
			if(StringUtils.isNotEmpty(so.getDuty().getCode())){
				criteria.add(Restrictions.eq("du.code", StringUtils.trim(so.getDuty().getCode())));
			}
			
			if(StringUtils.isNotEmpty(so.getDuty().getName())){
				criteria.add(Restrictions.ilike("du.name", "%"+StringUtils.trim(so.getDuty().getName())+"%"));
			}
		}
		
		if(so.getDepart()!=null){
			criteria.createAlias("depart", "de");
			
			if(so.getDepart().getId()!=null){
				criteria.add(Restrictions.eq("de.id", so.getDepart().getId()));
			}
			
			if(StringUtils.isNotEmpty(so.getDepart().getCode())){
				criteria.add(Restrictions.eq("de.code", StringUtils.trim(so.getDepart().getCode())));
			}
			
			if(StringUtils.isNotEmpty(so.getDepart().getName())){
				criteria.add(Restrictions.ilike("de.name", "%"+StringUtils.trim(so.getDepart().getName())+"%"));
			}
		}
		
		if(so.getSex()!=null){
			criteria.add(Restrictions.eq("sex", so.getSex()));
		}
		
		if(StringUtils.isNotEmpty(so.getIdcard())){
			criteria.add(Restrictions.eq("idcard",StringUtils.trim(so.getIdcard())));
		}
		
		if(StringUtils.isNotEmpty(so.getName())){
			criteria.add(Restrictions.eq("name",StringUtils.trim(so.getName())));
		}
		
		if(StringUtils.isNotEmpty(so.getCode())){
			criteria.add(Restrictions.eq("code",StringUtils.trim(so.getCode())));
		}
	}

	@Override
	public void setQueryParams(Query query, TeacherSO so) {
		
	}

	@Override
	public void switchVO2PO(Teacher po, TeacherSO so) {
		if(so.getDuty()!=null && so.getDuty().getId()!=null){
			SysDict duty=findById(SysDict.class, so.getDuty().getId());
			po.setDuty(duty);
		}
		
		if(so.getDepart()!=null && so.getDepart().getId()!=null){
			SysDict depart=findById(SysDict.class, so.getDepart().getId());
			po.setDepart(depart);
		}
		
		if(so.getSex()!=null){
			po.setSex(so.getSex());
		}
		
		if(StringUtils.isNotEmpty(so.getIdcard())){
			po.setIdcard(StringUtils.trim(so.getIdcard()));
		}
		
		if(StringUtils.isNotEmpty(so.getName())){
			po.setName(StringUtils.trim(so.getName()));
		}
		
		if(StringUtils.isNotEmpty(so.getCode())){
			po.setCode(StringUtils.trim(so.getCode()));
		}
		
		if(StringUtils.isNotEmpty(so.getPhone())){
			po.setPhone(StringUtils.trim(so.getPhone()));
		}
		
		if(StringUtils.isNotEmpty(so.getAddress())){
			po.setAddress(StringUtils.trim(so.getAddress()));
		}
		
		if(StringUtils.isNotEmpty(so.getMobile())){
			po.setMobile(StringUtils.trim(so.getMobile()));
		}
		
		if(StringUtils.isNotEmpty(so.getQq())){
			po.setQq(StringUtils.trim(so.getQq()));
		}
		
		if(StringUtils.isNotEmpty(so.getCaption())){
			po.setCaption(StringUtils.trim(so.getCaption()));
		}
		
		if(StringUtils.isNotEmpty(so.getPhoto())){
			po.setPhoto(StringUtils.trim(so.getPhoto()));
		}
		
		if(StringUtils.isNotEmpty(so.getEmail())){
			po.setEmail(StringUtils.trim(so.getEmail()));
		}
		
		if(StringUtils.isNotEmpty(so.getPostcode())){
			po.setPostcode(StringUtils.trim(so.getPostcode()));
		}
		
		
	}

}